/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.main;

import cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.common.RuntimeEnv;
import static cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.common.RuntimeEnv.jsonQueue;
import static cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.common.RuntimeEnv.requestDir;
import static cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.common.RuntimeEnv.responseDir;
import cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.config.Configuration;
import cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.handler.AudioDownloadHandler;
import cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.handler.ParserFileHandler;
import cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.handler.ParserFileHandler1;
import cn.ac.iie.di.ban.jr.wx.audio.dowloadserver.utils.ConfigureUtil;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class AudioDownloadServer {

//    public static int coreThreadPoolSize = 0;
//    public static int maxThreadPoolSize = 0;
//    public static int blockingQueueSize = 0;
//    public static String requestDir = "";
//    public static String respnseDir = "";
//    public static String mpost_dir = "";
//    public static ArrayBlockingQueue<String> jsonQueue = null;
//    public static int jsonQueueSize = 10;
//    public static int localFileSize = 0;
    static Logger logger = Logger.getLogger(AudioDownloadServer.class.getName());

    public static void main(String[] args) {
        try {
            init();
            multipleAudioDownloadThread();
//            AudioDownloadHandler audioDownloadHandler = new AudioDownloadHandler((String) RuntimeEnv.getParam(RuntimeEnv.responseDir));
//            Thread thread = new Thread(audioDownloadHandler);
//            thread.start();
//            parserFile();

        } catch (Exception e) {
            logger.fatal(e.getMessage(), e);
            System.exit(-1);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "configuration.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info("init successfully");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

    public static void parserFile() {
        String requestDir = (String) RuntimeEnv.getParam(RuntimeEnv.requestDir);
        if (!requestDir.endsWith("/")) {
            requestDir = requestDir + "/";
        }

        //美篇消息
        Thread mpostThread = new Thread(new ParserFileHandler(requestDir, jsonQueue));
        mpostThread.setName("###Parser_mpost");
        mpostThread.start();

    }

    public static void multipleAudioDownloadThread() {
        String requestDir = (String) RuntimeEnv.getParam(RuntimeEnv.requestDir);
        if (!requestDir.endsWith("/")) {
            requestDir = requestDir + "/";
        }
        Thread mpostThread = new Thread(new ParserFileHandler1(requestDir));
        mpostThread.setName("### ParserFileHandler");
        mpostThread.start();
    }
}
